home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / os / sprite / connection.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-26  |  27.1 KB  |  1,068 lines

  1. /***********************************************************
  2. Copyright 1987, 1989 by Digital Equipment Corporation, Maynard, Massachusetts,
  3. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Digital or MIT not be
  12. used in advertising or publicity pertaining to distribution of the
  13. software without specific, written prior permission.  
  14.  
  15. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  16. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  17. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  18. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  19. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21. SOFTWARE.
  22.  
  23. ******************************************************************/
  24. /* $XConsortium: connection.c,v 1.120 89/11/13 11:53:33 rws Exp $ */
  25. /*****************************************************************
  26.  *  Stuff to create connections --- OS dependent
  27.  *
  28.  *      EstablishNewConnections, CreateWellKnownSockets, ResetWellKnownSockets,
  29.  *      CloseDownConnection, CheckConnections, AddEnabledDevice,
  30.  *    RemoveEnabledDevice, OnlyListToOneClient,
  31.  *      ListenToAllClients,
  32.  *
  33.  *      (WaitForSomething is in its own file)
  34.  *
  35.  *      In this implementation, a client socket table is not kept.
  36.  *      Instead, what would be the index into the table is just the
  37.  *      file descriptor of the socket.  This won't work for if the
  38.  *      socket ids aren't small nums (0 - 2^8)
  39.  *
  40.  *****************************************************************/
  41.  
  42. #include <dbm.h>
  43. #undef NULL
  44. #include "X.h"
  45. #include "Xproto.h"
  46. #include <sys/param.h>
  47. #include <errno.h>
  48. #include "Xos.h"            /* for strings, file, time */
  49. #include <sys/socket.h>
  50.  
  51. #include <signal.h>
  52. #include <fcntl.h>
  53. #include <setjmp.h>
  54.  
  55. #ifdef hpux
  56. #include <sys/utsname.h>
  57. #include <sys/ioctl.h>
  58. #endif
  59.  
  60. #ifdef TCPCONN
  61. # include <netinet/in.h>
  62. # ifndef hpux
  63. #  ifdef apollo
  64. #   ifndef NO_TCP_H
  65. #    include <netinet/tcp.h>
  66. #   endif
  67. #  else
  68. #   include <netinet/tcp.h>
  69. #  endif
  70. # endif
  71. #endif
  72.  
  73. #ifdef UNIXCONN
  74. /*
  75.  * sites should be careful to have separate /tmp directories for diskless nodes
  76.  */
  77. #include <sys/un.h>
  78. #include <sys/stat.h>
  79. static int unixDomainConnection = -1;
  80. #endif
  81.  
  82. #ifdef SPRITEPDEVCONN
  83.  
  84. static int spritePdevConnection = -1;
  85. #endif 
  86.  
  87. #include <stdio.h>
  88. #include <sys/uio.h>
  89. #include "osstruct.h"
  90. #include "osdep.h"
  91. #include "opaque.h"
  92. #include "dixstruct.h"
  93.  
  94. #ifdef DNETCONN
  95. #include <netdnet/dn.h>
  96. #endif /* DNETCONN */
  97.  
  98. typedef long CCID;      /* mask of indices into client socket table */
  99.  
  100. #ifndef X_UNIX_PATH
  101. #ifdef hpux
  102. #define X_UNIX_DIR    "/usr/spool/sockets/X11"
  103. #define X_UNIX_PATH    "/usr/spool/sockets/X11/"
  104. #define OLD_UNIX_DIR    "/tmp/.X11-unix"
  105. #else
  106. #define X_UNIX_DIR    "/tmp/.X11-unix"
  107. #define X_UNIX_PATH    "/tmp/.X11-unix/X"
  108. #endif
  109. #endif
  110.  
  111. char *display;            /* The display number */
  112. int lastfdesc;            /* maximum file descriptor */
  113.  
  114. long WellKnownConnections;    /* Listener mask */
  115. long EnabledDevices[mskcnt];    /* mask for input devices that are on */
  116. long AllSockets[mskcnt];    /* select on this */
  117. long AllClients[mskcnt];    /* available clients */
  118. long LastSelectMask[mskcnt];    /* mask returned from last select call */
  119. long ClientsWithInput[mskcnt];    /* clients with FULL requests in buffer */
  120. long ClientsWriteBlocked[mskcnt];/* clients who cannot receive output */
  121. long OutputPending[mskcnt];    /* clients with reply/event data ready to go */
  122. long MaxClients = MAXSOCKS ;
  123. long OutputBufferSize = BUFSIZ; /* output buffer size (must be > 0) */
  124. long NConnBitArrays = mskcnt;
  125. Bool NewOutputPending;        /* not yet attempted to write some new output */
  126. Bool AnyClientsWriteBlocked;    /* true if some client blocked on write */
  127.  
  128. static Bool SendSignal;        /* send SIGUSR1 to parent process */
  129. static int ParentProcess;
  130.  
  131. static Bool debug_conns = FALSE;
  132.  
  133. static int SavedAllClients[mskcnt];
  134. static int SavedAllSockets[mskcnt];
  135. static int SavedClientsWithInput[mskcnt];
  136. static int GrabInProgress = 0;
  137.  
  138. int ConnectionTranslation[MAXSOCKS];
  139. extern ClientPtr NextAvailableClient();
  140.  
  141. extern int AutoResetServer();
  142. extern int GiveUp();
  143. extern XID CheckAuthorization();
  144. static void CloseDownFileDescriptor(), ErrorConnMax();
  145. extern void FreeOsBuffers(), ResetOsBuffers();
  146.  
  147. #ifdef XDMCP
  148. void XdmcpOpenDisplay(), XdmcpInit(), XdmcpReset(), XdmcpCloseDisplay();
  149. #endif
  150.  
  151. #ifdef TCPCONN
  152. static int
  153. open_tcp_socket ()
  154. {
  155.     struct sockaddr_in insock;
  156.     int request;
  157.     int retry;
  158. #ifndef SO_DONTLINGER
  159. #ifdef SO_LINGER
  160.     static int linger[2] = { 0, 0 };
  161. #endif /* SO_LINGER */
  162. #endif /* SO_DONTLINGER */
  163.  
  164. #if defined(AIX) && defined(IBM_OS_HAS_HFT) && !defined(IBM_OS_HAS_X_QUEUE)
  165. #ifndef FORCE_DISPLAY_NUM
  166.     extern int AIXTCPSocket;
  167.     if (AIXTCPSocket>=0) {
  168.         request= AIXTCPSocket;
  169.     } else
  170. #endif /* FORCE_DISPLAY_NUM */
  171. #endif /* AIX && etc. */
  172.     if ((request = socket (AF_INET, SOCK_STREAM, 0)) < 0) 
  173.     {
  174.     Error ("Creating TCP socket");
  175.     return -1;
  176.     } 
  177. #ifdef SO_REUSEADDR
  178.     /* Necesary to restart the server without a reboot */
  179. #ifdef hpux
  180.     set_socket_option (request, SO_REUSEADDR);
  181. #else
  182.     {
  183.     int one = 1;
  184.     setsockopt(request, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int));
  185.     }
  186. #endif
  187. #endif /* SO_REUSEADDR */
  188. #if defined(AIX) && defined(IBM_OS_HAS_HFT) && !defined(IBM_OS_HAS_X_QUEUE)
  189. #ifndef FORCE_DISPLAY_NUMBER
  190.     if (AIXTCPSocket<0)
  191. #endif
  192. #endif
  193.     {
  194.     bzero ((char *)&insock, sizeof (insock));
  195.     insock.sin_family = AF_INET;
  196.     insock.sin_port = htons ((unsigned short)(X_TCP_PORT + atoi (display)));
  197.     insock.sin_addr.s_addr = htonl(INADDR_ANY);
  198.     retry = 20;
  199.     while (bind(request, (struct sockaddr *) &insock, sizeof (insock)))
  200.     {
  201.     if (--retry == 0) {
  202.         Error ("Binding TCP socket");
  203.         close (request);
  204.         return -1;
  205.     }
  206. #ifdef SO_REUSEADDR
  207.     sleep (1);
  208. #else
  209.     sleep (10);
  210. #endif /* SO_REUSEDADDR */
  211.     }
  212.     }
  213. #ifdef SO_DONTLINGER
  214.     if(setsockopt (request, SOL_SOCKET, SO_DONTLINGER, (char *)NULL, 0))
  215.     Error ("Setting TCP SO_DONTLINGER");
  216. #else
  217. #ifdef SO_LINGER
  218.     if(setsockopt (request, SOL_SOCKET, SO_LINGER,
  219.            (char *)linger, sizeof(linger)))
  220.     Error ("Setting TCP SO_LINGER");
  221. #endif /* SO_LINGER */
  222. #endif /* SO_DONTLINGER */
  223.     if (listen (request, 5)) {
  224.     Error ("TCP Listening");
  225.     close (request);
  226.     return -1;
  227.     }
  228.     return request;
  229. }
  230. #endif /* TCPCONN */
  231.  
  232. #ifdef UNIXCONN
  233.  
  234. static struct sockaddr_un unsock;
  235.  
  236. static int
  237. open_unix_socket ()
  238. {
  239.     int oldUmask;
  240.     int request;
  241.  
  242.     unsock.sun_family = AF_UNIX;
  243.     oldUmask = umask (0);
  244. #ifdef X_UNIX_DIR
  245.     mkdir (X_UNIX_DIR, 0777);
  246.     chmod (X_UNIX_DIR, 0777);
  247. #endif
  248.     strcpy (unsock.sun_path, X_UNIX_PATH);
  249.     strcat (unsock.sun_path, display);
  250. #ifdef hpux
  251.     {  
  252.         /*    The following is for backwards compatibility
  253.          *    with old HP clients. This old scheme predates the use
  254.      *    of the /usr/spool/sockets directory, and uses hostname:display
  255.       *    in the /tmp/.X11-unix directory
  256.          */
  257.         struct utsname systemName;
  258.     static char oldLinkName[256];
  259.  
  260.         uname(&systemName);
  261.         strcpy(oldLinkName, OLD_UNIX_DIR);
  262.         mkdir(oldLinkName, 0777);
  263.         chown(oldLinkName, 2, 3);
  264.         strcat(oldLinkName, "/");
  265.         strcat(oldLinkName, systemName.nodename);
  266.         strcat(oldLinkName, display);
  267.         unlink(oldLinkName);
  268.         symlink(unsock.sun_path, oldLinkName);
  269.     }
  270. #endif    /* hpux */
  271.     unlink (unsock.sun_path);
  272.     if ((request = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) 
  273.     {
  274.     Error ("Creating Unix socket");
  275.     return -1;
  276.     } 
  277.     if (bind(request, (struct sockaddr *)&unsock, strlen(unsock.sun_path)+2))
  278.     {
  279.     Error ("Binding Unix socket");
  280.     close (request);
  281.     return -1;
  282.     }
  283.     if (listen (request, 5))
  284.     {
  285.     Error ("Unix Listening");
  286.     close (request);
  287.     return -1;
  288.     }
  289.     (void)umask(oldUmask);
  290.     return request;
  291. }
  292. #endif /*UNIXCONN */
  293.  
  294. #ifdef SPRITEPDEVCONN
  295.  
  296. /*
  297.  * Template for the pseudo-device through which we communicate. Given to
  298.  * one of the printf functions and expects two arguments: the name of the
  299.  * local host and the display number we're using.
  300.  */
  301. #define    DEVICE_TEMPLATE    "/hosts/%s/X%s"
  302.  
  303. static char deviceName[1024];
  304.  
  305. static int
  306. open_sprite_pdev ()
  307. {
  308.     int oldUmask;
  309.     int request;
  310.     char hostname[256];
  311.  
  312.     /*
  313.      * Create the pseudo-device, making sure it's readable and writable
  314.      * by everyone.
  315.      */
  316.  
  317.     gethostname(hostname, 256);
  318.     hostname[255] = 0;
  319.     sprintf (deviceName, DEVICE_TEMPLATE, hostname, display);
  320.     oldUmask = umask (0);
  321.     request = PdevCreate(deviceName);
  322.     (void)umask(oldUmask);
  323.     return request;
  324. }
  325. #endif /* SPRITEPDEVCONN */
  326.  
  327. #ifdef hpux
  328. /*
  329.  * hpux returns EOPNOTSUPP when using getpeername on a unix-domain
  330.  * socket.  In this case, smash the socket address with the address
  331.  * used to bind the connection socket and return success.
  332.  */
  333. hpux_getpeername(fd, from, fromlen)
  334.     int    fd;
  335.     struct sockaddr *from;
  336.     int            *fromlen;
  337. {
  338.     int        ret;
  339.     int        len;
  340.  
  341.     ret = getpeername(fd, from, fromlen);
  342.     if (ret == -1 && errno == EOPNOTSUPP)
  343.     {
  344.     ret = 0;
  345.     len = strlen(unsock.sun_path)+2;
  346.     if (len > *fromlen)
  347.         len = *fromlen;
  348.     bcopy ((char *) &unsock, (char *) from, len);
  349.     *fromlen = len;
  350.     }
  351.     return ret;
  352. }
  353.  
  354. #define getpeername(fd, from, fromlen)    hpux_getpeername(fd, from, fromlen)
  355.  
  356. #endif
  357.  
  358. #ifdef DNETCONN
  359. static int
  360. open_dnet_socket ()
  361. {
  362.     int request;
  363.     struct sockaddr_dn dnsock;
  364.  
  365.     if ((request = socket (AF_DECnet, SOCK_STREAM, 0)) < 0) 
  366.     {
  367.     Error ("Creating DECnet socket");
  368.     return -1;
  369.     } 
  370.     bzero ((char *)&dnsock, sizeof (dnsock));
  371.     dnsock.sdn_family = AF_DECnet;
  372.     sprintf(dnsock.sdn_objname, "X$X%d", atoi (display));
  373.     dnsock.sdn_objnamel = strlen(dnsock.sdn_objname);
  374.     if (bind (request, (struct sockaddr *) &dnsock, sizeof (dnsock)))
  375.     {
  376.     Error ("Binding DECnet socket");
  377.     close (request);
  378.     return -1;
  379.     }
  380.     if (listen (request, 5))
  381.     {
  382.     Error ("DECnet Listening");
  383.     close (request);
  384.     return -1;
  385.     }
  386.     return request;
  387. }
  388. #endif /* DNETCONN */
  389.  
  390. /*****************
  391.  * CreateWellKnownSockets
  392.  *    At initialization, create the sockets to listen on for new clients.
  393.  *****************/
  394.  
  395. void
  396. CreateWellKnownSockets()
  397. {
  398.     int        request, i;
  399.  
  400.     CLEARBITS(AllSockets);
  401.     CLEARBITS(AllClients);
  402.     CLEARBITS(LastSelectMask);
  403.     CLEARBITS(ClientsWithInput);
  404.  
  405.     for (i=0; i<MAXSOCKS; i++) ConnectionTranslation[i] = 0;
  406.     
  407. #ifdef    hpux
  408.     lastfdesc = _NFILE - 1;
  409. #else
  410.     lastfdesc = getdtablesize() - 1;
  411. #endif    /* hpux */
  412.  
  413.     if (lastfdesc > MAXSOCKS)
  414.     {
  415.     lastfdesc = MAXSOCKS;
  416.     if (debug_conns)
  417.         ErrorF( "GOT TO END OF SOCKETS %d\n", MAXSOCKS);
  418.     }
  419.  
  420.     WellKnownConnections = 0;
  421. #ifdef TCPCONN
  422.     if ((request = open_tcp_socket ()) != -1) {
  423.     WellKnownConnections |= (1L << request);
  424.     DefineSelf (request);
  425.     }
  426. #endif /* TCPCONN */
  427. #ifdef UNIXCONN
  428.     if ((request = open_unix_socket ()) != -1) {
  429.     WellKnownConnections |= (1L << request);
  430.     unixDomainConnection = request;
  431.     }
  432. #endif /* UNIXCONN */
  433. #ifdef SPRITEPDEVCONN
  434.     if ((request = open_sprite_pdev ()) != -1) {
  435.     WellKnownConnections |= (1L << request);
  436.     spritePdevConnection = request;
  437.     }
  438. #endif /* SPRITEPDEVCONN */
  439. #ifdef DNETCONN
  440.     if ((request = open_dnet_socket ()) != -1) {
  441.     WellKnownConnections |= (1L << request);
  442.     DefineSelf (request);
  443.     }
  444. #endif /* DNETCONN */
  445.     if (WellKnownConnections == 0)
  446.         FatalError ("Cannot establish any listening sockets");
  447.     signal (SIGPIPE, SIG_IGN);
  448.     signal (SIGHUP, AutoResetServer);
  449.     signal (SIGINT, GiveUp);
  450.     signal (SIGTERM, GiveUp);
  451.     AllSockets[0] = WellKnownConnections;
  452.     ResetHosts(display);
  453.     /*
  454.      * Magic:  If SIGUSR1 was set to SIG_IGN when
  455.      * the server started, assume that either
  456.      *
  457.      *  a- The parent process is ignoring SIGUSR1
  458.      *
  459.      * or
  460.      *
  461.      *  b- The parent process is expecting a SIGUSR1
  462.      *     when the server is ready to accept connections
  463.      *
  464.      * In the first case, the signal will be harmless,
  465.      * in the second case, the signal will be quite
  466.      * useful
  467.      */
  468.     if (signal (SIGUSR1, SIG_IGN) == SIG_IGN)
  469.     SendSignal = TRUE;
  470.     ParentProcess = getppid ();
  471.     if (SendSignal) {
  472.     if (ParentProcess > 0) {
  473.         kill (ParentProcess, SIGUSR1);
  474.     }
  475.     }
  476. #ifdef XDMCP
  477.     XdmcpInit ();
  478. #endif
  479. }
  480.  
  481. void
  482. ResetWellKnownSockets ()
  483. {
  484.     ResetOsBuffers();
  485. #ifdef UNIXCONN
  486.     if (unixDomainConnection != -1)
  487.     {
  488.     /*
  489.      * see if the unix domain socket has disappeared
  490.      */
  491.     struct stat    statb;
  492.  
  493.     if (stat (unsock.sun_path, &statb) == -1 ||
  494.         (statb.st_mode & S_IFMT) != S_IFSOCK)
  495.     {
  496.         ErrorF ("Unix domain socket %s trashed, recreating\n",
  497.         unsock.sun_path);
  498.         (void) unlink (unsock.sun_path);
  499.         (void) close (unixDomainConnection);
  500.         WellKnownConnections &= ~(1L << unixDomainConnection);
  501.         unixDomainConnection = open_unix_socket ();
  502.         if (unixDomainConnection != -1)
  503.         WellKnownConnections |= (1L << unixDomainConnection);
  504.     }
  505.     }
  506. #endif /* UNIXCONN */
  507.     ResetAuthorization ();
  508.     ResetHosts(display);
  509.     /*
  510.      * See above in CreateWellKnownSockets about SIGUSR1
  511.      */
  512.     if (SendSignal) {
  513.     if (ParentProcess > 0) {
  514.         kill (ParentProcess, SIGUSR1);
  515.     }
  516.     }
  517.     /*
  518.      * restart XDMCP
  519.      */
  520. #ifdef XDMCP
  521.     XdmcpReset ();
  522. #endif
  523. }
  524.  
  525. /*****************************************************************
  526.  * ClientAuthorized
  527.  *
  528.  *    Sent by the client at connection setup:
  529.  *                typedef struct _xConnClientPrefix {
  530.  *                   CARD8    byteOrder;
  531.  *                   BYTE    pad;
  532.  *                   CARD16    majorVersion, minorVersion;
  533.  *                   CARD16    nbytesAuthProto;    
  534.  *                   CARD16    nbytesAuthString;   
  535.  *                 } xConnClientPrefix;
  536.  *
  537.  *         It is hoped that eventually one protocol will be agreed upon.  In the
  538.  *        mean time, a server that implements a different protocol than the
  539.  *        client expects, or a server that only implements the host-based
  540.  *        mechanism, will simply ignore this information.
  541.  *
  542.  *****************************************************************/
  543.  
  544. char * 
  545. ClientAuthorized(client, proto_n, auth_proto, string_n, auth_string)
  546.     ClientPtr client;
  547.     char *auth_proto, *auth_string;
  548.     unsigned short proto_n, string_n;
  549. {
  550.     register OsCommPtr priv;
  551.     union {
  552.     struct sockaddr sa;
  553. #ifdef UNIXCONN
  554.     struct sockaddr_un un;
  555. #endif /* UNIXCONN */
  556. #ifdef TCPCONN
  557.     struct sockaddr_in in;
  558. #endif /* TCPCONN */
  559. #ifdef DNETCONN
  560.     struct sockaddr_dn dn;
  561. #endif /* DNETCONN */
  562.     } from;
  563.     int    fromlen = sizeof (from);
  564.     XID     auth_id;
  565.  
  566.     auth_id = CheckAuthorization (proto_n, auth_proto,
  567.                   string_n, auth_string);
  568.  
  569.     priv = (OsCommPtr)client->osPrivate;
  570. #ifdef SPRITEPDEVCONN
  571.     if (auth_id == (XID) ~0L &&
  572.     !InvalidPdevConn (priv->fd)) {
  573.     auth_id = (XID) 0;
  574.     }
  575. #endif
  576.     if (auth_id == (XID) ~0L && 
  577.     getpeername (priv->fd, &from.sa, &fromlen) != -1 &&
  578.         !InvalidHost (&from.sa, fromlen))
  579.     {
  580.     auth_id = (XID) 0;
  581.     }
  582.  
  583.     if (auth_id == (XID) ~0L)
  584.     return "Client is not authorized to connect to Server";
  585.  
  586.     priv->auth_id = auth_id;
  587.     priv->conn_time = 0;
  588.  
  589.     /* At this point, if the client is authorized to change the access control
  590.      * list, we should getpeername() information, and add the client to
  591.      * the selfhosts list.  It's not really the host machine, but the
  592.      * true purpose of the selfhosts list is to see who may change the
  593.      * access control list.
  594.      */
  595.     return((char *)NULL);
  596. }
  597.  
  598. /*****************
  599.  * EstablishNewConnections
  600.  *    If anyone is waiting on listened sockets, accept them.
  601.  *    Returns a mask with indices of new clients.  Updates AllClients
  602.  *    and AllSockets.
  603.  *****************/
  604.  
  605. void
  606. EstablishNewConnections()
  607. {
  608.     long readyconnections;     /* mask of listeners that are ready */
  609.     int curconn;                  /* fd of listener that's ready */
  610.     register int newconn;         /* fd of new client */
  611.     long connect_time;
  612.     register int i;
  613.     register ClientPtr client;
  614.     register OsCommPtr oc;
  615.  
  616. #ifdef TCP_NODELAY
  617.     union {
  618.     struct sockaddr sa;
  619. #ifdef UNIXCONN
  620.     struct sockaddr_un un;
  621. #endif /* UNIXCONN */
  622. #ifdef TCPCONN
  623.     struct sockaddr_in in;
  624. #endif /* TCPCONN */
  625. #ifdef DNETCONN
  626.     struct sockaddr_dn dn;
  627. #endif /* DNETCONN */
  628.     } from;
  629.     int    fromlen;
  630. #endif /* TCP_NODELAY */
  631.  
  632.     readyconnections = (LastSelectMask[0] & WellKnownConnections);
  633.     if (!readyconnections)
  634.     return;
  635.     connect_time = GetTimeInMillis();
  636.     /* kill off stragglers */
  637.     for (i=1; i<currentMaxClients; i++)
  638.     {
  639.     if (client = clients[i])
  640.     {
  641.         oc = (OsCommPtr)(client->osPrivate);
  642.         if (oc && (oc->conn_time != 0) &&
  643.         (connect_time - oc->conn_time) >= TimeOutValue)
  644.         CloseDownClient(client);     
  645.     }
  646.     }
  647.     while (readyconnections) 
  648.     {
  649.     curconn = ffs (readyconnections) - 1;
  650.     readyconnections &= ~(1 << curconn);
  651. #ifdef SPRITEPDEVCONN
  652.     if (PdevIsMaster(curconn)) {
  653.         if ((newconn = PdevAccept(curconn)) < 0) 
  654.         continue;
  655.     } else
  656. #endif
  657.     if ((newconn = accept (curconn,
  658.                   (struct sockaddr *) NULL, 
  659.                   (int *)NULL)) < 0) 
  660.         continue;
  661. #ifdef TCP_NODELAY
  662.     fromlen = sizeof (from);
  663.     if (!getpeername (newconn, &from.sa, &fromlen))
  664.     {
  665.         if (fromlen && (from.sa.sa_family == AF_INET)) 
  666.         {
  667.         int mi = 1;
  668.         setsockopt (newconn, IPPROTO_TCP, TCP_NODELAY,
  669.                (char *)&mi, sizeof (int));
  670.         }
  671.     }
  672. #endif /* TCP_NODELAY */
  673. #ifdef    hpux
  674.     /*
  675.      * HPUX does not have  FNDELAY
  676.      */
  677.     {
  678.         int    arg;
  679.         arg = 1;
  680.         ioctl(newconn, FIOSNBIO, &arg);
  681.     }
  682. #else
  683.     fcntl (newconn, F_SETFL, FNDELAY);
  684. #endif /* hpux */
  685.     oc = (OsCommPtr)xalloc(sizeof(OsCommRec));
  686.     if (!oc)
  687.     {
  688.         xfree(oc);
  689.         ErrorConnMax(newconn);
  690.         close(newconn);
  691.         continue;
  692.     }
  693.     if (GrabInProgress)
  694.     {
  695.         BITSET(SavedAllClients, newconn);
  696.         BITSET(SavedAllSockets, newconn);
  697.     }
  698.     else
  699.     {
  700.         BITSET(AllClients, newconn);
  701.         BITSET(AllSockets, newconn);
  702.     }
  703.     oc->fd = newconn;
  704.     oc->input = (ConnectionInputPtr)NULL;
  705.     oc->output = (ConnectionOutputPtr)NULL;
  706.     oc->conn_time = connect_time;
  707.     if ((newconn < lastfdesc) &&
  708.         (client = NextAvailableClient((pointer)oc)))
  709.     {
  710.         ConnectionTranslation[newconn] = client->index;
  711.     }
  712.     else
  713.     {
  714.         ErrorConnMax(newconn);
  715.         CloseDownFileDescriptor(oc);
  716.     }
  717. #ifdef XDMCP
  718.     /* indicate to Xdmcp protocol that we've opened new client */
  719.     XdmcpOpenDisplay(newconn);
  720. #endif /* XDMCP */
  721.     }
  722. }
  723.  
  724. #define NOROOM "Maximum number of clients reached"
  725.  
  726. /************
  727.  *   ErrorConnMax
  728.  *     Fail a connection due to lack of client or file descriptor space
  729.  ************/
  730.  
  731. static void
  732. ErrorConnMax(fd)
  733.     register int fd;
  734. {
  735.     xConnSetupPrefix csp;
  736.     char pad[3];
  737.     struct iovec iov[3];
  738.     char byteOrder = 0;
  739.     int whichbyte = 1;
  740.     struct timeval waittime;
  741.     long mask[mskcnt];
  742.  
  743.     /* if these seems like a lot of trouble to go to, it probably is */
  744.     waittime.tv_sec = BOTIMEOUT / MILLI_PER_SECOND;
  745.     waittime.tv_usec = (BOTIMEOUT % MILLI_PER_SECOND) *
  746.                (1000000 / MILLI_PER_SECOND);
  747.     CLEARBITS(mask);
  748.     BITSET(mask, fd);
  749.     (void)select(fd + 1, (int *) mask, (int *) NULL, (int *) NULL, &waittime);
  750.     /* try to read the byte-order of the connection */
  751. #ifdef SPRITEPDEVCONN
  752.     if (PdevIsPdevConn(fd)) {
  753.     (void)read(fd, &byteOrder, 1);
  754.     } else 
  755. #endif
  756.     (void)read(fd, &byteOrder, 1);
  757.     if ((byteOrder == 'l') || (byteOrder == 'B'))
  758.     {
  759.     csp.success = xFalse;
  760.     csp.lengthReason = sizeof(NOROOM) - 1;
  761.     csp.length = (sizeof(NOROOM) + 2) >> 2;
  762.     csp.majorVersion = X_PROTOCOL;
  763.     csp.minorVersion = X_PROTOCOL_REVISION;
  764.     if (((*(char *) &whichbyte) && (byteOrder == 'B')) ||
  765.         (!(*(char *) &whichbyte) && (byteOrder == 'l')))
  766.     {
  767.         swaps(&csp.majorVersion, whichbyte);
  768.         swaps(&csp.minorVersion, whichbyte);
  769.         swaps(&csp.length, whichbyte);
  770.     }
  771.     iov[0].iov_len = sz_xConnSetupPrefix;
  772.     iov[0].iov_base = (char *) &csp;
  773.     iov[1].iov_len = csp.lengthReason;
  774.     iov[1].iov_base = NOROOM;
  775.     iov[2].iov_len = (4 - (csp.lengthReason & 3)) & 3;
  776.     iov[2].iov_base = pad;
  777. #ifdef SPRITEPDEVCONN
  778.     if (PdevIsPdevConn(fd)) {
  779.         (void)PdevWritev(fd, iov, 3);
  780.     } else
  781. #endif
  782.     (void)writev(fd, iov, 3);
  783.     }
  784. }
  785.  
  786. /************
  787.  *   CloseDownFileDescriptor:
  788.  *     Remove this file descriptor and it's I/O buffers, etc.
  789.  ************/
  790.  
  791. static void
  792. CloseDownFileDescriptor(oc)
  793.     register OsCommPtr oc;
  794. {
  795.     int connection = oc->fd;
  796.  
  797. #ifdef SPRITEPDEVCONN
  798.     if (PdevIsPdevConn(connection)) {
  799.     PdevClose(connection);
  800.     } else
  801. #endif
  802.     close(connection);
  803.     FreeOsBuffers(oc);
  804.     BITCLEAR(AllSockets, connection);
  805.     BITCLEAR(AllClients, connection);
  806.     BITCLEAR(ClientsWithInput, connection);
  807.     if (GrabInProgress)
  808.     {
  809.     BITCLEAR(SavedAllSockets, connection);
  810.     BITCLEAR(SavedAllClients, connection);
  811.     BITCLEAR(SavedClientsWithInput, connection);
  812.     }
  813.     BITCLEAR(ClientsWriteBlocked, connection);
  814.     if (!ANYSET(ClientsWriteBlocked))
  815.         AnyClientsWriteBlocked = FALSE;
  816.     BITCLEAR(OutputPending, connection);
  817.     xfree(oc);
  818. }
  819.  
  820. /*****************
  821.  * CheckConections
  822.  *    Some connection has died, go find which one and shut it down 
  823.  *    The file descriptor has been closed, but is still in AllClients.
  824.  *    If would truly be wonderful if select() would put the bogus
  825.  *    file descriptors in the exception mask, but nooooo.  So we have
  826.  *    to check each and every socket individually.
  827.  *****************/
  828.  
  829. void
  830. CheckConnections()
  831. {
  832.     long        mask;
  833.     long        tmask[mskcnt]; 
  834.     register int    curclient, curoff;
  835.     int            i;
  836.     struct timeval    notime;
  837.     int r;
  838.  
  839.     notime.tv_sec = 0;
  840.     notime.tv_usec = 0;
  841.  
  842.     for (i=0; i<mskcnt; i++)
  843.     {
  844.     mask = AllClients[i];
  845.         while (mask)
  846.         {
  847.         curoff = ffs (mask) - 1;
  848.          curclient = curoff + (i << 5);
  849.             CLEARBITS(tmask);
  850.             BITSET(tmask, curclient);
  851.             r = select (curclient + 1, (int *)tmask, (int *)NULL, (int *)NULL, 
  852.             ¬ime);
  853.             if (r < 0) {
  854.         CloseDownClient(clients[ConnectionTranslation[curclient]]);
  855.         fprintf(stderr, "Closing down client %d\n", 
  856.             ConnectionTranslation[curclient]);
  857.         }
  858.         mask &= ~(1 << curoff);
  859.     }
  860.     }    
  861. }
  862.  
  863.  
  864. /*****************
  865.  * CloseDownConnection
  866.  *    Delete client from AllClients and free resources 
  867.  *****************/
  868.  
  869. CloseDownConnection(client)
  870.     ClientPtr client;
  871. {
  872.     OsCommPtr oc = (OsCommPtr)client->osPrivate;
  873.  
  874.     if (oc->output && oc->output->count)
  875.     FlushClient(client, oc, (char *)NULL, 0);
  876.     ConnectionTranslation[oc->fd] = 0;
  877. #ifdef XDMCP
  878.     XdmcpCloseDisplay(oc->fd);
  879. #endif
  880.     CloseDownFileDescriptor(oc);
  881.     client->osPrivate = (pointer)NULL;
  882. }
  883.  
  884.  
  885. AddEnabledDevice(fd)
  886.     int fd;
  887. {
  888.     BITSET(EnabledDevices, fd);
  889.     BITSET(AllSockets, fd);
  890. }
  891.  
  892.  
  893. RemoveEnabledDevice(fd)
  894.     int fd;
  895. {
  896.     BITCLEAR(EnabledDevices, fd);
  897.     BITCLEAR(AllSockets, fd);
  898. }
  899.  
  900. /*****************
  901.  * OnlyListenToOneClient:
  902.  *    Only accept requests from  one client.  Continue to handle new
  903.  *    connections, but don't take any protocol requests from the new
  904.  *    ones.  Note that if GrabInProgress is set, EstablishNewConnections
  905.  *    needs to put new clients into SavedAllSockets and SavedAllClients.
  906.  *    Note also that there is no timeout for this in the protocol.
  907.  *    This routine is "undone" by ListenToAllClients()
  908.  *****************/
  909.  
  910. OnlyListenToOneClient(client)
  911.     ClientPtr client;
  912. {
  913.     OsCommPtr oc = (OsCommPtr)client->osPrivate;
  914.     int connection = oc->fd;
  915.  
  916.     if (! GrabInProgress)
  917.     {
  918.     COPYBITS (ClientsWithInput, SavedClientsWithInput);
  919.         BITCLEAR (SavedClientsWithInput, connection);
  920.     if (GETBIT(ClientsWithInput, connection))
  921.     {
  922.         CLEARBITS(ClientsWithInput);        
  923.         BITSET(ClientsWithInput, connection);
  924.     }
  925.     else
  926.         {
  927.         CLEARBITS(ClientsWithInput);        
  928.     }
  929.     COPYBITS(AllSockets, SavedAllSockets);
  930.     COPYBITS(AllClients, SavedAllClients);
  931.  
  932.     UNSETBITS(AllSockets, AllClients);
  933.     BITSET(AllSockets, connection);
  934.     CLEARBITS(AllClients);
  935.     BITSET(AllClients, connection);
  936.     GrabInProgress = client->index;
  937.     }
  938. }
  939.  
  940. /****************
  941.  * ListenToAllClients:
  942.  *    Undoes OnlyListentToOneClient()
  943.  ****************/
  944.  
  945. ListenToAllClients()
  946. {
  947.     if (GrabInProgress)
  948.     {
  949.     ORBITS(AllSockets, AllSockets, SavedAllSockets);
  950.     ORBITS(AllClients, AllClients, SavedAllClients);
  951.     ORBITS(ClientsWithInput, ClientsWithInput, SavedClientsWithInput);
  952.     GrabInProgress = 0;
  953.     }    
  954. }
  955.  
  956. /****************
  957.  * IgnoreClient
  958.  *    Removes one client from input masks.
  959.  *    Must have cooresponding call to AttendClient.
  960.  ****************/
  961.  
  962. static long IgnoredClientsWithInput[mskcnt];
  963. static long IgnoredSavedClientsWithInput[mskcnt];
  964.  
  965. IgnoreClient (client)
  966.     ClientPtr    client;
  967. {
  968.     OsCommPtr oc = (OsCommPtr)client->osPrivate;
  969.     int connection = oc->fd;
  970.  
  971.     if (GETBIT (ClientsWithInput, connection))
  972.     BITSET(IgnoredClientsWithInput, connection);
  973.     else
  974.     BITCLEAR(IgnoredClientsWithInput, connection);
  975.     BITCLEAR(ClientsWithInput, connection);
  976.     BITCLEAR(AllSockets, connection);
  977.     BITCLEAR(AllClients, connection);
  978.     if (GrabInProgress)
  979.     {
  980.         if (GETBIT (SavedClientsWithInput, connection))
  981.         BITSET(IgnoredSavedClientsWithInput, connection);
  982.         else
  983.         BITCLEAR(IgnoredSavedClientsWithInput, connection);
  984.     BITCLEAR(SavedClientsWithInput, connection);
  985.     BITCLEAR(SavedAllSockets, connection);
  986.     BITCLEAR(SavedAllClients, connection);
  987.     }
  988. }
  989.  
  990. /****************
  991.  * AttendClient
  992.  *    Adds one client back into the input masks.
  993.  ****************/
  994.  
  995. AttendClient (client)
  996.     ClientPtr    client;
  997. {
  998.     OsCommPtr oc = (OsCommPtr)client->osPrivate;
  999.     int connection = oc->fd;
  1000.  
  1001.     if (!GrabInProgress || GrabInProgress == client->index)
  1002.     {
  1003.         BITSET(AllClients, connection);
  1004.         BITSET(AllSockets, connection);
  1005.         if (GETBIT (IgnoredClientsWithInput, connection))
  1006.         BITSET(ClientsWithInput, connection);
  1007.     }
  1008.     else
  1009.     {
  1010.     BITSET(SavedAllClients, connection);
  1011.     BITSET(SavedAllSockets, connection);
  1012.     if (GETBIT(IgnoredSavedClientsWithInput, connection))
  1013.         BITSET(SavedClientsWithInput, connection);
  1014.     }
  1015. }
  1016.  
  1017. #if defined(AIX) && defined(IBM_OS_HAS_HFT) && !defined(IBM_OS_HAS_X_QUEUE)
  1018.  
  1019. static int grabbingClient;
  1020. static int reallyGrabbed;
  1021.  
  1022. /****************
  1023. * DontListenToAnybody:
  1024. *   Don't listen to requests from any clients. Continue to handle new
  1025. *   connections, but don't take any protocol requests from anybody.
  1026. *   We have to take care if there is already a grab in progress, though.
  1027. *   Undone by PayAttentionToClientsAgain. We also have to be careful
  1028. *   not to accept any more input from the currently dispatched client.
  1029. *   we do this be telling dispatch it is time to yield.
  1030.  
  1031. *   We call this when the server loses access to the glass
  1032. *   (user hot-keys away).  This looks like a grab by the 
  1033. *   server itself, but gets a little tricky if there is already
  1034. *   a grab in progress.
  1035. ******************/
  1036.  
  1037. void
  1038. DontListenToAnybody()
  1039. {
  1040.     if (!GrabInProgress) {
  1041.     COPYBITS(ClientsWithInput, SavedClientsWithInput);
  1042.     COPYBITS(AllSockets, SavedAllSockets);
  1043.     COPYBITS(AllClients, SavedAllClients);
  1044.     GrabInProgress = TRUE;
  1045.     reallyGrabbed = FALSE;
  1046.     } else {
  1047.     reallyGrabbed = TRUE;
  1048.     }
  1049.     CLEARBITS(ClientsWithInput);
  1050.     UNSETBITS(AllSockets, AllClients);
  1051.     CLEARBITS(AllClients);
  1052.     isItTimeToYield = TRUE;
  1053. }
  1054.  
  1055. void
  1056. PayAttentionToClientsAgain()
  1057. {
  1058.     if (reallyGrabbed) {
  1059.     BITSET(AllSockets, grabbingClient);
  1060.     BITSET(AllClients, grabbingClient);
  1061.     } else {
  1062.     ListenToAllClients();
  1063.     }
  1064.     reallyGrabbed = FALSE;
  1065. }
  1066.  
  1067. #endif
  1068.